import htmlDownCheck.HtmlProError;
import htmlPicProcesser.ProcessBatchHtmlPic;
import htmlPicReader.ReadBatchHtmlPic;
import htmlProcesser.ProcessBatchHtml;
import htmlReader.HtmlReadControler;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

import tvDataPersistence.TVDataDAO;
import tvDataPersistence.TVDataImportDB;
import util.DownTVException;

public class HtmlDownTV {

	private static Logger log = Logger.getLogger("downtv");
	
	private static final String DATASAVEPATH = "data/";
	
	private static final String errorFilePath = "htmlError.txt";
	
	private static final String UNKNOWNPICNUMNM = "unKnownPicNum.dat";
	
	private String chDirNm = null;    //20120430-20120506\
	
	/**
	 * @param args
	 * @throws UnsupportedEncodingException 
	 * @throws FileNotFoundException 
	 */
	public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
		PropertyConfigurator.configure("log4j.properties");
		
		HtmlDownTV htmlDownTV = new HtmlDownTV();
		htmlDownTV.htmlDownAndProcess();
		
		List<String> errors = HtmlProError.getErrorHtmls();
		int i = 0;
		for(; i < 3; i++){
			if(!errors.isEmpty()){
				for(Iterator<String> it = errors.iterator(); it.hasNext();){
					File tmp = new File(it.next());
					if(!tmp.delete()){
						tmp.delete();
					}
				}
				log.info("error num: " + errors.size());
			}else {
				break;
			}
			errors.clear();
			htmlDownTV.htmlDownAndProcess();
		}
		log.info("process error times: " + i);
		
		File errorFile = new File(errorFilePath);		
		if(!errors.isEmpty()){
			PrintWriter writer = new PrintWriter(errorFile, "UTF-8");
			writer.println("error num: " + errors.size());
			for(Iterator<String> it = errors.iterator(); it.hasNext();){
				File tmp = new File(it.next());
				writer.println(tmp.getPath());
				if(!tmp.delete()){
					tmp.delete();
				}
			}
			writer.close();	
			log.info("create error file: " + errorFilePath);
		}else {
			errorFile.delete();
			log.info("delete error file: " + errorFilePath);
		}
		
		TVDataDAO tvDataDAO = new TVDataDAO();
		try {
			tvDataDAO.importDataDirtoDB(DATASAVEPATH + htmlDownTV.chDirNm);
			log.info("create tvData.sql file ok.");
		} catch (IOException e) {
			log.error(e, e);
			e.printStackTrace();
		}
		
		TVDataImportDB tvDataImportDB = new TVDataImportDB();
		try {
			tvDataImportDB.importMySQL();
			log.info("import tv to MySQL ok.");
		} catch (Exception e) {
			log.error(e, e);
			e.printStackTrace();
		}
	}
	
	private final void htmlDownAndProcess(){
		try {
			log.info("start to down and process TV from html! time: " + new Date());
			HtmlReadControler htmlReadCtrl = new HtmlReadControler();					
			ProcessBatchHtmlPic processBatchHtmlPic = new ProcessBatchHtmlPic();
			
			htmlReadCtrl.work();
			chDirNm = htmlReadCtrl.getChannelDirNm();
			StringBuffer unKnPicNumPath = new StringBuffer(DATASAVEPATH + chDirNm
					+ UNKNOWNPICNUMNM);
			ProcessBatchHtml processHtml = new ProcessBatchHtml(chDirNm);			
			processHtml.work();
			ReadBatchHtmlPic readBatchHtmlPic = new ReadBatchHtmlPic(chDirNm);
			
			while(true){
				File file = new File(unKnPicNumPath.toString());
				if(file.exists()){
					log.info(unKnPicNumPath + "is exists!");
					readBatchHtmlPic.work();
					processBatchHtmlPic.work();
					processHtml.work();
				}else {
					log.info(unKnPicNumPath + "is not exists! process over!");
					break;
				}
			}
			
			log.info("down TV and process from html over! time: " + new Date());
		} catch (IOException e) {
			log.error(e, e);
			e.printStackTrace();
		} catch (DownTVException e) {
			log.error(e, e);
			e.printStackTrace();
		}
	}
}